package jCross.gui;


import jCross.intel.TableData;
import jCross.io.IOUtil;

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JPanel;

public class MainPanel extends JPanel {
	public boolean drawGrid = false, drawDoubleGrid = false;
	@Override
	public void paint(Graphics g) {
		g.setColor(getBackground());
		g.fillRect(0, 0, getWidth(), getHeight());
		
		TableData tableData = TableData.getInstance();
		if(IOUtil.wd <= 0 || IOUtil.ht <= 0 || tableData.wd == 0) {
			return;
		}

		int nrCellsX = IOUtil.wd, nrCellsY = IOUtil.ht;
		int cellDimX = (getWidth() - 5) / nrCellsX, cellDimY = (getHeight() - 5) / nrCellsY;

		if(drawGrid) {
			g.setColor(Color.black);
			for(int i = 0; i <= nrCellsX; i++) {
				g.drawLine(i * cellDimX, 0, i * cellDimX, nrCellsY * cellDimY);
				if(i % 5 == 0 && drawDoubleGrid) {
					g.drawLine(i * cellDimX + 1, 0, i * cellDimX + 1, nrCellsY * cellDimY);
				}
			}
	
			for(int i = 0; i <= nrCellsY; i++) {
				g.drawLine(0, i * cellDimY, nrCellsX * cellDimX, i * cellDimY);
				if(i % 5 == 0 && drawDoubleGrid) {
					g.drawLine(0, i * cellDimY + 1, nrCellsX * cellDimX, i * cellDimY + 1);
				}
			}
		}
		
		for(int i = 0; i < nrCellsX; i++) {
			for(int j = 0; j < nrCellsY; j++) {
				if(tableData.data[i][j] == TableData.CHECK) {
					g.setColor(new Color(80, 80, 160));
					g.fillOval(i * cellDimX + cellDimX / 6, j * cellDimY + cellDimY / 6, cellDimX * 2 / 3, cellDimY * 2 / 3);
				} else if(tableData.data[i][j] == TableData.EMPTY) {
					g.setColor(new Color(0, 0, 0));
					//g.drawOval(i * cellDimX + cellDimX * 3 / 7, j * cellDimY + cellDimY * 3 / 7, cellDimX * 1 / 7, cellDimY * 1 / 7);
					g.drawString("?", i * cellDimX + cellDimX * 2 / 3, j * cellDimY + cellDimY * 2 / 3);
				} else {
					g.setColor(new Color(250, 0, 0));
					g.drawOval(i * cellDimX + cellDimX / 3, j * cellDimY + cellDimY / 3, cellDimX * 1 / 3, cellDimY * 1 / 3);
				}
			}
		}
	}
}
